package com.czk.ssachatclientchatmodel.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Author: chenzhangkun
 * @CreateTime: 2025-10-11
 * @Description:
 * @Version: 1.0
 */
@RestController
public class ChatClientModelController {
    /**
     * 对话模型，对应阿里云百炼平台
     * chatModel可以自动注入，不需要配置
     */
    @Resource
    private ChatModel chatModel;
    /**
     * 需要自己初始化，不能直接注入
     * 依赖chatModel对象接口
     * 实际工作中两者混合使用
     */
    @Resource
    private ChatClient chatClient;

    /**
     * @return java.lang.String
     * @Author chenzhangkun
     * @Description 通用调用，阻塞性返回
     * @Date 20:43 2025/10/12
     * @Param [msg]
     **/
    @GetMapping(value = "/hello/dochat")
    public String doChat(@RequestParam(name = "msg", defaultValue = "你是谁") String msg) {
        return chatModel.call(msg);
    }

    /**
     * 流式返回
     *
     * @param msg
     * @return
     */
    @GetMapping(value = "/hello/dochat/stream")
    public Flux<String> doChatStream(@RequestParam(name = "msg", defaultValue = "你是谁") String msg) {
        return chatModel.stream(msg);
    }


    @GetMapping(value = "/hello/dochat/chatclient/stream")
    public Flux<String> doChatClientStream(@RequestParam(name = "msg", defaultValue = "自贡市") String msg) {
        return chatClient.prompt().user(msg).stream().content();
    }

}
